﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SGraphicer
{
    public partial class Settings : Form
    {
        public int WindowWidth = 800;
        public int WindowHeight = 600;
        public bool Fullscreen = false;

        public Image ImageLeft;
        public Image ImageRight;
        public Bitmap RedBall;

        public bool Enable3DMouse = false;
        public bool SwapEyes = false;

        public int MinDimension = 0;

        public Settings()
        {
            InitializeComponent();

            if ((Screen.PrimaryScreen.Bounds.Width != 800 &&
                Screen.PrimaryScreen.Bounds.Height != 600) &&
                (Screen.PrimaryScreen.Bounds.Width != 1024 &&
                Screen.PrimaryScreen.Bounds.Height != 768) &&
                (Screen.PrimaryScreen.Bounds.Width != 1280 && (
                Screen.PrimaryScreen.Bounds.Height != 1024 ||
                Screen.PrimaryScreen.Bounds.Height != 720)))// * 1024
            {
                cbxResolutions.Items.Add(Screen.PrimaryScreen.Bounds.Width.ToString() + "x" + Screen.PrimaryScreen.Bounds.Height.ToString());
            }

            cbxResolutions.SelectedIndex = 0;
        }

        public void SetImages(Image Left, Image Right)
        {
            ImageLeft = Left;
            ImageRight = Right;

            pictureBoxLeft.Image = ImageLeft;
            pictureBoxRight.Image = ImageRight;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (cbxResolutions.SelectedIndex < 0 || cbxResolutions.SelectedIndex > 6)
                cbxResolutions.SelectedIndex = 0;

            if (cbxResolutions.SelectedIndex == 0)
            {
                WindowWidth = 800;
                WindowHeight = 600;
            }
            else if (cbxResolutions.SelectedIndex == 1)
            {
                WindowWidth = 1024;
                WindowHeight = 768;
            }
            else if (cbxResolutions.SelectedIndex == 2)
            {
                WindowWidth = 1280;
                WindowHeight = 720;
            }
            else if (cbxResolutions.SelectedIndex == 3)
            {
                WindowWidth = 1280;
                WindowHeight = 1024;
            }
            else if (cbxResolutions.SelectedIndex == 4)
            {
                WindowWidth = 1650;
                WindowHeight = 1050;
            }
            else if (cbxResolutions.SelectedIndex == 5)
            {
                WindowWidth = 1980;
                WindowHeight = 1080;
            }
            else if (cbxResolutions.SelectedIndex == 6)
            {
                WindowWidth = Convert.ToInt16(Screen.PrimaryScreen.Bounds.Width);
                WindowHeight = Convert.ToInt16(Screen.PrimaryScreen.Bounds.Height);
            }

            Console.WriteLine(cbxResolutions.SelectedIndex + " " + WindowWidth + " " + WindowHeight);

            Fullscreen = cbxFullScreen.Checked;
            Enable3DMouse = cbx3DMouse.Checked;
            SwapEyes = cbxSwapEyes.Checked;

            ImageLeft = FitImage(ImageLeft);

            ImageRight = FitImage(ImageRight);

            pictureBoxLeft.Image = ImageLeft;
            pictureBoxRight.Image = ImageRight;

            /*ImageLeft.Save("left.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            ImageRight.Save("right.bmp", System.Drawing.Imaging.ImageFormat.Bmp);*/

            MinDimension = Math.Min(WindowWidth, WindowHeight);

            RedBall = new Bitmap(MinDimension, MinDimension);

            Console.WriteLine(MinDimension);

            /*using (Graphics g = Graphics.FromImage(RedBall))
            {
                g.Clear(Color.Transparent);
                g.FillPolygon(Brushes.Red, new Point[] { new Point(0, 0), new Point(MinDimension, MinDimension / 2), new Point(MinDimension / 2, MinDimension) });
                //g.FillEllipse(Brushes.Red, new Rectangle(0, 0, MinDimension, MinDimension));
                g.Flush();
            }

            RedBall.Save("cursor.bmp", System.Drawing.Imaging.ImageFormat.Bmp);*/

            this.DialogResult = DialogResult.OK;
            Close();
        }

        private Image FitImage(Image SourceImage)
        {
            Image newImage = new Bitmap(WindowWidth, WindowHeight);
            Graphics gfx = Graphics.FromImage(newImage);
            gfx.Clear(Color.Black);

            int iX = 0, iY = 0, iWidth = WindowWidth, iHeight = WindowHeight;

            /*if (ImageLeft_.Width / (float)ImageLeft_.Height < WindowWidth / (float)WindowHeight)
            {
                iWidth = WindowWidth;
                iX = 0;
                iHeight = 
            }*/

            gfx.DrawImage(SourceImage, new Rectangle(iX, iY, iWidth, iHeight));

            gfx.Flush();
            gfx.Dispose();

            return newImage;
        }

        private void pictureBoxLeft_Click(object sender, EventArgs e)
        {
            if (openFileDialogLeft.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ImageLeft = Image.FromFile(openFileDialogLeft.FileName);
                    pictureBoxLeft.Image = ImageLeft;
                }
                catch (Exception) { }
            }
        }

        private void pictureBoxRight_Click(object sender, EventArgs e)
        {
            if (openFileDialogRight.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ImageRight = Image.FromFile(openFileDialogRight.FileName);
                    pictureBoxRight.Image = ImageRight;
                }
                catch (Exception) { }
            }
        }

        private void cbxResolutions_KeyDown(object sender, KeyEventArgs e)
        {
            btnOk_Click(null, null);
        }
    }
}
